package com.iris.live.services.report.handlers;

import com.iris.live.services.data.models.ReportConfig;
import com.iris.live.services.report.data.IReportDataSource;
import com.iris.live.services.report.data.ReportContext;
import com.iris.live.services.report.data.timeline.TimeUnit;
import com.iris.live.services.report.data.timeline.TimelineContext;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Component;

import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SalesFunnelRowHandler
 *
 * @author sallygo
 * @date 2017/12/28 16:21
 */
@Component("SalesFunnelRowHandler")
public class SalesFunnelRowHandler extends BaseReportRowHandler{
    private static Logger logger = LoggerFactory.getLogger(SalesFunnelRowHandler.class);

    @Resource(name = "SalesFunnelClueDataSource")
    private IReportDataSource salesFunnelClueDataSource;

    @Override
    public boolean isSupport() {
        return false;
    }

    @Override
    public void handle(Map reportRow, ReportConfig config, ReportContext context, List<Map> resultSet4ReportRow, Map<String, Map>  all) throws Exception{
        if(context instanceof TimelineContext){
            handleTimLine(reportRow, config, context, resultSet4ReportRow, all);
        }
    }


    /**
     * 时间维度
     * @param reportRow
     * @param config
     * @param context
     * @param resultSet4ReportRow
     * @param all
     * @throws Exception
     */
    private void handleTimLine(Map reportRow, ReportConfig config, ReportContext context, List<Map> resultSet4ReportRow, Map<String, Map>  all) throws Exception{
        String fullPath = this.getFullPath(config);
        TimelineContext bContext = (TimelineContext) context;
        String code = config.getAinCode();
        Map params = context.getParams();

        if ("线索数".equalsIgnoreCase(code)) {

            List<ReportConfig> configs = new ArrayList<>();
            configs.add(config);
            List list =  salesFunnelClueDataSource.getData(context,configs);
            Integer clueYear = 0,clueQ1 = 0,clueQ2 = 0,clueQ3 = 0,clueQ4 = 0;
            Map<String,Integer> clueMap = new HashMap<>();
            String yearStr = params.get("selectYear").toString();
            clueMap.put(code+"_"+yearStr,clueYear);
            clueMap.put(code+"_Q1",clueQ1);
            clueMap.put(code+"_Q2",clueQ2);
            clueMap.put(code+"_Q3",clueQ3);
            clueMap.put(code+"_Q4",clueQ4);
            for (Object o : list) {
                Map map = (Map) o;
                Integer tempClueNum = Integer.parseInt(map.get("clue_num").toString());
                Integer tempNewClueNum = Integer.parseInt(map.get("new_clue_num").toString());

                if ("01".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempClueNum);
                    clueMap.put(code+"_Q1",clueMap.get(code+"_Q1")+ tempClueNum);
                }
                if ("02".equalsIgnoreCase(map.get("date").toString())
                        || "03".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q1",clueMap.get(code+"_Q1")+ tempNewClueNum);
                }
                if ("04".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q2",clueMap.get(code+"_Q2")+ tempClueNum);
                }
                if ("05".equalsIgnoreCase(map.get("date").toString())
                        || "06".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q2",clueMap.get(code+"_Q2")+ tempNewClueNum);
                }
                if ("07".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q3",clueMap.get(code+"_Q3")+ tempClueNum);
                }
                if ("08".equalsIgnoreCase(map.get("date").toString())
                        || "09".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q3",clueMap.get(code+"_Q3")+ tempNewClueNum);
                }
                if ("10".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q4",clueMap.get(code+"_Q4")+ tempClueNum);
                }
                if ("11".equalsIgnoreCase(map.get("date").toString())
                        || "12".equalsIgnoreCase(map.get("date").toString())) {
                    clueMap.put(code+"_"+yearStr,clueMap.get(code+"_"+yearStr)+ tempNewClueNum);
                    clueMap.put(code+"_Q4",clueMap.get(code+"_Q4")+ tempNewClueNum);
                }
            }

            for(TimeUnit tu : bContext.getTimeUnits()){
                Map cell = (Map) reportRow.get(fullPath + "_" + tu.getValue());
                // 年特殊处理 e.g. 2017  = 1月线索 + 后11月新增线索
                // 季特殊处理 e.g. Q4    = 10月线索 + 后2月新增线索
                if (tu.getType().equals(TimeUnit.TimeType.YEAR)) {
                    cell.put(config.getCategory() + "Value", clueMap.get(code+"_"+yearStr));
                    logger.debug("YEAR");
                } else if (tu.getType().equals(TimeUnit.TimeType.QUARTER)) {
                    if ("Q1".equalsIgnoreCase(cell.get("displayRow").toString())) {
                        cell.put(config.getCategory() + "Value", clueMap.get(code+"_Q1"));
                    } else if ("Q2".equalsIgnoreCase(cell.get("displayRow").toString())) {
                        cell.put(config.getCategory() + "Value", clueMap.get(code+"_Q2"));
                    } else if ("Q3".equalsIgnoreCase(cell.get("displayRow").toString())) {
                        cell.put(config.getCategory() + "Value", clueMap.get(code+"_Q3"));
                    } else if ("Q4".equalsIgnoreCase(cell.get("displayRow").toString())) {
                        cell.put(config.getCategory() + "Value", clueMap.get(code+"_Q4"));
                    }
                    logger.debug("QUARTER");
                }
                logger.debug("handleTimLine");
            }
        }
    }
}
